package top.sssd.auth.service.impl.permission;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.sssd.auth.service.permission.PermissionService;
import top.sssd.auth.mapper.permission.PermissionMapper;
import top.sssd.auth.model.permission.converter.PermissionConverter;
import top.sssd.auth.model.permission.entity.Permission;
import top.sssd.auth.model.permission.request.PermissionQueryReq;
import top.sssd.auth.model.permission.request.PermissionSaveOrUpdateReq;
import top.sssd.auth.model.permission.response.PermissionResp;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Override
    public Page<PermissionResp> queryPage(PermissionQueryReq permissionQueryReq) {
        Page<Permission> page = new Page<>(permissionQueryReq.getPageNum(), permissionQueryReq.getPageSize());
        Page<Permission> resultPage = commonQuery(permissionQueryReq).page(page);
        List<PermissionResp> resultList = resultPage.getRecords().parallelStream()
                .map(PermissionConverter.INSTANCE::entityToResp)
                .toList();
        return new Page<PermissionResp>()
                .setTotal(resultPage.getTotal())
                .setCurrent(resultPage.getCurrent())
                .setSize(resultPage.getSize())
                .setRecords(resultList);
    }

    @Override
    public List<PermissionResp> queryPermissionRespListByPermissionReq(PermissionQueryReq PermissionQueryReq) {
        List<Permission> list = commonQuery(PermissionQueryReq).list();
        return list.parallelStream()
                .map(PermissionConverter.INSTANCE::entityToResp)
                .toList();
    }

    private LambdaQueryChainWrapper<Permission> commonQuery(PermissionQueryReq permissionQueryReq) {
        return this.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getId()), Permission::getId, permissionQueryReq.getId())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getName()), Permission::getName, permissionQueryReq.getName())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getCode()), Permission::getCode, permissionQueryReq.getCode())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getType()), Permission::getType, permissionQueryReq.getType())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getUrl()), Permission::getUrl, permissionQueryReq.getUrl())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getMethod()), Permission::getMethod, permissionQueryReq.getMethod())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getParentId()), Permission::getParentId, permissionQueryReq.getParentId())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getOrderNum()), Permission::getOrderNum, permissionQueryReq.getOrderNum())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getIcon()), Permission::getIcon, permissionQueryReq.getIcon())
                .eq(ObjectUtil.isNotEmpty(permissionQueryReq.getStatus()), Permission::getStatus, permissionQueryReq.getStatus())
                .orderByDesc(Permission::getUpdatedTime);
    }

    @Override
    public PermissionResp getPermissionRespById(Serializable id) {
        Permission permission = this.getById(id);
        if (Objects.nonNull(permission)) {
            return PermissionConverter.INSTANCE.entityToResp(permission);
        }
        throw new RuntimeException("权限表不存在");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean savePermissionReq(PermissionSaveOrUpdateReq permissionSaveOrUpdateReq) {
        Permission permission = PermissionConverter.INSTANCE.saveOrUpdatePermissionReqToEntity(permissionSaveOrUpdateReq);
        return this.save(permission);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updatePermissionReq(PermissionSaveOrUpdateReq permissionSaveOrUpdateReq) {
        Permission permission = PermissionConverter.INSTANCE.saveOrUpdatePermissionReqToEntity(permissionSaveOrUpdateReq);
        return this.updateById(permission);
    }
}

